<template>
  <div class="my-container">
    <div class="header not-login" v-if="!user">
      <div class="logindiv" @click="$router.push('/login')">
        <img class="avatar" src="~@/assets/mobile.png" alt="" />
        <span class="text">登录/注册</span>
      </div>
    </div>
    <div class="header userinfo" v-else>
      <div class="baseInfo">
        <div class="left">
          <van-image class="avatar" round :src="userInfo.photo" />

          <span class="title">{{ userInfo.name }}</span>
        </div>
        <van-button round plain size="mini" class="right" to="/updateProfile">
          资料编辑</van-button
        >
      </div>
      <div class="user-stats">
        <div class="data-item">
          <span class="count">{{ userInfo.art_count }}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.fans_count }}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.like_count }}</span>
          <span class="text">获赞</span>
        </div>
        <div class="data-item">
          <span class="count">{{ userInfo.follow_count }}</span>
          <span class="text">关注</span>
        </div>
      </div>
    </div>

    <van-grid :column-num="2" class="grid-nav" clickable>
      <van-grid-item class="grid-item">
        <i slot="icon" class="iconfont icon-shoucang"></i>
        <span slot="text" class="text"> 收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <i slot="icon" class="iconfont icon-lishi"></i>
        <span slot="text" class="text"> 历史</span>
      </van-grid-item>
    </van-grid>

    <div class="fndiv">
      <van-cell title="小智同学" is-link />
      <van-cell title="浏览" is-link />
      <van-cell class="loginoutbtn" @click="logOut" v-if="user">
        <span slot="title">退出登录</span>
      </van-cell>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfoAPI } from '@/api'
export default {
  name: 'MyIndex',
  computed: {
    ...mapState(['user'])
  },
  created () {
    this.getUserInfo()
  },
  data () {
    return {
      userInfo: {}
    }
  },
  methods: {
    logOut () {
      this.$dialog
        .confirm({
          title: '确认退出吗'
        })
        .then(() => {
          this.$store.commit('setUser', null)
        })
        .catch(err => {
          console.log(err)
        })
    },
    async getUserInfo () {
      try {
        const { data: res } = await getUserInfoAPI()
        // console.log(res)
        this.userInfo = res.data
      } catch (error) {
        console.dir('my=>' + error)
        this.$toast.fail('用户数据获取失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.my-container {
  .header {
    height: 361px;
    background-image: url('~@/assets/banner.png');
    background-size: cover;
  }

  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .logindiv {
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;

      img {
        width: 132px;
        height: 132px;
        margin-bottom: 15px;
      }
      .text {
        font-size: 28px;
        color: #fff;
      }
    }
  }
  .userinfo {
    .baseInfo {
      height: 231px;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        justify-content: center;
        align-items: center;
        .avatar {
          width: 120px;
          height: 120px;
          margin-right: 23px;
          border: 4px solid #fff;
        }
        .title {
          font-size: 30px;
          color: #fff;
        }
      }
    }
    .user-stats {
      display: flex;
      .data-item {
        height: 130px;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }

  .grid-nav {
    .grid-item {
      height: 141px;
      .iconfont {
        font-size: 45px;
        color: tomato;
      }
      .text {
        font-size: 28px !important;
      }
    }
  }

  .fndiv {
    margin-top: 10px;
    .loginoutbtn {
      display: flex;
      justify-content: center;
      align-items: center;
      margin-top: 10px;
      text-align: center;
    }
  }
}
</style>
